<?php
namespace Home\Controller;

class AuthController extends CommonController {
	
	//权限首页
	public function index() {
		$this->display();
	}
	
	/**
	 * 栏目管理
	 */
	//添加栏目
	public function addCate() {
		$this->display();
	}
	
	//添加栏目表单处理
	public function addCateHandle() {
		
	}
	
	//栏目列表
	public function cateList() {
		$this->display();
	}
	
	/**
	 * 角色管理
	 */
	//新增角色
	public function addGroup() {
		$this->display();
	}
	
	//新增角色表单处理
	public function addGroupHandle() {
		if(!IS_POST){
			$this->error('非法请求');
		}
		
		$data = array(
			'title' => I('title'),
		);
		if(M('Auth_group')->add($data)) {
			$this->success('添加角色成功', U('Auth/groupList'));
		}else {
			$this->error("添加角色失败");
		}
	}
	
	//删除角色
	public function delGroup() {
		$id = I('id');
		if(M('Auth_group')->where('id='. $id)->delete()){
			$this->success('删除角色成功', U('Auth/groupList'));
		}else {
			$this->error('删除角色失败');
		}
	}
	
	//修改角色
	public function updGroup() {
		$this->id = I('id');
		$this->title = M('Auth_group')->where('id='. I('id'))->getField('title');
		$this->display();
	}
	
	//修改角色处理
	public function updGroupHandle() {
		$data = array(
			'id'	=> I('id'),
			'title' => I('title')
		);
		if(M('Auth_group')->save($data)) {
			$this->success('修改角色成功', U('Auth/groupList'));
		}else {
			$this->error('修改角色失败', U('Auth/groupList'));
		}
	}
	
	//角色列表
	public function groupList() {
		$this->grouList = M('Auth_group')->select();
		$this->display();
	}

	/**
	 * 权限管理
	 */
	//添加权限
	public function addRule() {
		$this->display();
	}
	
	//添加权限表单处理
	public function addRuleHandle() {
		if(!IS_POST){
			$this->error('非法请求');
		}
		
		$data = array(
			'name'  => I('name'),
			'title' => I('title')
		);
		
		if(M('Auth_rule')->add($data)){
			$this->success('添加权限成功', U('Auth/ruleList'));
		}else{
			$this->error('添加权限失败', U('Auth/ruleList'));
		}
	}
	
	//删除权限
	public function delRule() {
		$id = I('id');
		if(M('Auth_rule')->where('id='. $id)->delete()) {
			$this->success('删除权限成功', U('Auth/ruleList'));
		}else {
			$this->error('删除权限失败', U('Auth/ruleList'));
		}
	}
	
	//修改权限
	public function updRule() {
		$id = I('id');
		$this->rule = M('Auth_rule')->where('id='. $id)->find();
		$this->display();
	}
	
	//修改权限表单处理
	public function updRuleHandle() {
		if(!IS_POST){
			$this->error('非法请求');
		}
	
		$data = array(
			'id'  => I('id'),
			'name'  => I('name'),
			'title' => I('title')
		);
		
		if(M('Auth_rule')->save($data)){
			$this->success('修改权限成功', U('Auth/ruleList'));
		}else{
			$this->error('修改权限失败');
		}
	}

	//权限列表
	public function ruleList() {
		$this->rules = M('Auth_rule')->select();
		$this->display();
	}
	
	/**
	 * 权限分配
	 */
	//分配权限
	public function setRule() {
		$this->id = I('id');
		$rules = M('Auth_group')->where('id='. I('id'))->getField('rules');
		$this->rules = explode(',', $rules);
		$this->rule = M('Auth_rule')->select();
		$this->display();
	}
	
	//分配权限处理
	public function setRuleHandle() {
		if(!IS_POST){
			$this->error('非法请求');
		}
		
		$id = I('id');
		$rule = I('rule');
		$rule = implode(',', $rule);
		$data = array(
			'rules' => $rule,
		);
		if(M('Auth_group')->where('id='. $id)->save($data)) {
			$this->success('分配权限成功', U('Auth/groupList'));
		}else {
			$this->error('分配权限失败');
		}
	}
	
	/**
	 * 用户管理
	 */
	//添加用户
	public function addUser() {
		$this->group = M('Auth_group')->select();
		$this->display();
	}
	
	//添加用户处理
	public function addUserHandle() {
		$data = array(
			'username' => I('username'),
			'password' => md5(I('pwd')),
		);
		$uid = M('User')->add($data);
		
		$access = array(
			'uid' 		=> $uid,
			'group_id'	=> I('gid'),
		);
		
		if(M('Auth_group_access')->add($access)) {
			$this->success('新增用户成功', U('Auth/userList'));
		}else {
			$this->error('新增用户失败', U('Auth/userList'));
		}
	}
	
	//删除用户
	public function delUser() {
		$id = I('id');
		if(M('User')->where('id='. $id)->delete()) {
			$this->success('删除用户成功', U('Auth/userList'));
		}else {
			$this->error('删除用户失败', U('Auth/userList'));
		}
	}
	
	//修改用户
	public function updUser() {
		$this->id = I('id');
		$this->username = M('User')->where('id='. I('id'))->getField('username');
		$this->group = M('Auth_group')->select();
		$this->display();
	}
	
	//修改用户处理
	public function updUserHandle() {
		$data = array(
			'id'	   => I('id'),
			'username' => I('username'),
			'password' => md5(I('pwd')),
		);
		
		if(!(M('User')->save($data))) {
			$this->error('修改用户错误', U('Auth/userList'));
		}else {
			$this->success('修改用户成功', U('Auth/userList'));
		}
		
	}
	
	//用户列表
	public function userList() {
		$this->userList = D('UserRelation')->relation(true)->select();
		$this->display();
	}
	
}
?>